package com.tencent.crazypenguin.dao;




import com.tencent.crazypenguin.bean.Comment;
import com.tencent.crazypenguin.config.HbmUtil;
import com.tencent.crazypenguin.config.ResultType;
import org.hibernate.Criteria;
import org.hibernate.QueryException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.Iterator;
import java.util.List;


public class CommentDao extends BaseDao<Comment> {
	
	/**
	 * 保存评分表数据
	 * @param comment
	 * @return
	 */
	public int insCommentData(Comment comment) {
		Session s = null;
		try{
			s = HbmUtil.getSession();
			s.beginTransaction();
			s.save(comment);
			s.getTransaction().commit();
			return ResultType.SUCEESS;
		}catch(Exception e){
			e.printStackTrace();
			return ResultType.EXCEPTION;
			
		}finally{
			if(s!=null){
				s.close();
			}

		}

	}

	/***
	 * 根据评分用户id返回给这个用户好评的用户列表和时间
	 * @param id
	 */
	public List<Comment> queryEvaluateeList(long id,String sort,String order){
		try {
			Criteria c = HbmUtil.getSession().createCriteria(Comment.class);

			c.add(Restrictions.or(Restrictions.eq("id_docom", id),Restrictions.eq("id_getcom", id)));
			if (sort != null && order != null) {
				if ("asc".equals(order)) {
					c.addOrder(Order.asc(sort));
				} else {
					c.addOrder(Order.desc(sort));
				}
			} else {
				// 默认按照ID升序排序
				c.addOrder(Order.asc("id"));
			}
			List<Comment> list = c.list();
			return list;

		} catch (QueryException ex) {
			throw ex;
		}
	}
}
